//-----------------------------------------------------------------------
// <copyright file="ResponsibleOfficeDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class ResponsibleOfficeDAOBase
    {
        #region Common methods
        public virtual ResponsibleOffice CreateResponsibleOfficeFromReader(IDataReader reader)
        {
            ResponsibleOffice item = new ResponsibleOffice();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ResponsibleID"))) item.ResponsibleID = (int)reader["ResponsibleID"];
                if (!reader.IsDBNull(reader.GetOrdinal("DefaultToOfficeID"))) item.DefaultToOfficeID = (int)reader["DefaultToOfficeID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateResponsibleOfficeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateResponsibleOffice methods
            
        public virtual void CreateResponsibleOffice(ResponsibleOffice responsibleOffice)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesInsert");
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsibleOffice.ResponsibleID);
                dbCommandWrapper.AddInParameter("@DefaultToOfficeID", DbType.Int32, responsibleOffice.DefaultToOfficeID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateResponsibleOfficeException, ex);
            }
        }

        #endregion

        #region UpdateResponsibleOffice methods
        
        public virtual void UpdateResponsibleOffice(ResponsibleOffice responsibleOffice)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesUpdate");            
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsibleOffice.ResponsibleID);
                dbCommandWrapper.AddInParameter("@DefaultToOfficeID", DbType.Int32, responsibleOffice.DefaultToOfficeID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateResponsibleOfficeException, ex);
            }
        }
        
        #endregion

        #region DeleteResponsibleOffice methods
        public virtual void DeleteResponsibleOffice(int responsibleID, int defaultToOfficeID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesDelete");
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsibleID);
                dbCommandWrapper.AddInParameter("@DefaultToOfficeID", DbType.Int32, defaultToOfficeID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteResponsibleOfficeException, ex);
            }
        }

        #endregion

        #region GetResponsibleOffice methods
        
        public virtual ResponsibleOffice GetResponsibleOffice(int responsibleID, int defaultToOfficeID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesGet");
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsibleID);
                dbCommandWrapper.AddInParameter("@DefaultToOfficeID", DbType.Int32, defaultToOfficeID);
                
                ResponsibleOffice responsibleOffice =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        responsibleOffice = CreateResponsibleOfficeFromReader(reader);
                        reader.Close();
                    }
                }
                return responsibleOffice;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetResponsibleOfficeException, ex);
            }
        }
        
        #endregion

        #region GetResponsibleOffices methods
        public virtual ResponsibleOfficeCollection GetResponsibleOffices(ResponsibleOfficeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsibleOfficesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                ResponsibleOfficeCollection responsibleOfficeCollection = new ResponsibleOfficeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        ResponsibleOffice responsibleOffice = CreateResponsibleOfficeFromReader(reader);
                        responsibleOfficeCollection.Add(responsibleOffice);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return responsibleOfficeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetResponsibleOfficesException, ex);
            }
        }
        
        public virtual ResponsibleOfficeCollection GetResponsibleOffices(ResponsibleOfficeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetResponsibleOffices(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}